﻿//Função para serializar checkbox
(function ($) {

    $.fn.serialize = function (options) {
        return $.param(this.serializeArray(options));
    };

    $.fn.serializeArray = function (options) {
        var o = $.extend({
            checkboxesAsBools: false
        }, options || {});

        var rselectTextarea = /select|textarea/i;
        var rinput = /text|hidden|password|search/i;

        return this.map(function () {
            return this.elements ? $.makeArray(this.elements) : this;
        })
        .filter(function () {
            return this.name && !this.disabled &&
                (this.checked
                || (o.checkboxesAsBools && this.type === 'checkbox')
                || rselectTextarea.test(this.nodeName)
                || rinput.test(this.type));
        })
            .map(function (i, elem) {
                var val = $(this).val();
                return val == null ?
                null :
                $.isArray(val) ?
                $.map(val, function (val, i) {
                    return { name: elem.name, value: val };
                }) :
             {
                 name: elem.name,
                 value: (o.checkboxesAsBools && this.type === 'checkbox') ? //moar ternaries!
                        (this.checked ? 'true' : 'false') :
                        val
             };
            }).get();
    };

})(jQuery);
//Valida o form
(function ($) {
    //Parêmetros: Título da modal, action para redirecionar, regras da validação, mensagens de erro
    $.fn.validateform = function (alertTitle, alertUrl, rules, messages) {      
        return this.each(function () {
            $(this).validate({
                rules: rules,
                messages: messages,
                onfocusout: false,
                errorElement: 'span', //default input error message container
                errorClass: 'help-block help-block-error', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.form-group').addClass('has-error'); // set error class to the control group
                },
                unhighlight: function (element) { // revert the change done by hightlight
                    $(element)
                        .closest('.form-group').removeClass('has-error'); // set error class to the control group
                },
                submitHandler: function (form) {   
                    var $form = $(form);
                    $.ajax({
                        type: $form.attr('method'),
                        url: $form.attr('action'),
                        data: $form.serialize({ checkboxesAsBools: true }),
                        success: function (data) {
                            alert('teste');
                            //if (data.erro) {
                            //    AlertMetro("Erro",data.erro,"#");
                            //} else {
                            //    AlertMetro(alertTitle, "Registro salvo com sucesso.", alertUrl);
                            //}
                        }
                    });
                }
            });
        });
    }
})(jQuery);